package curd

import (
	"github.com/gin-gonic/gin"
	// "go.uber.org/zap"
	// "goskeleton/app/global/variable"
	"goskeleton/app/model/work"
	contactPersonCurd "goskeleton/app/service/employee/curd"
	projectCurd "goskeleton/app/service/project/curd"
)

type WorkCurd struct {
	WorkModel *work.WorkModel
}

func CreateWorkCurdFactory() *WorkCurd {
	return &WorkCurd{WorkModel: work.CreateWorkFactory("")}
}

func (e *WorkCurd) InsertData(c *gin.Context) bool {
	return e.WorkModel.InsertData(c)
}

func (e *WorkCurd) UpdateData(c *gin.Context) bool {
	return e.WorkModel.UpdateData(c)
}

func (e *WorkCurd) ListData(
	limits, limitItems int,
	order string,
	settlementStatus,
	settlementMethod,
	paymentMethod,
	invoiceStatus int,
	settlementDateStart,
	settlementDateEnd,
	invoiceDateStart,
	invoiceDateEnd,
	paymentDateStart,
	paymentDateEnd string,
	departmentEmployeeIds []int,
) (totalCounts int64, tmp []*work.WorkModel) {
	totalCounts, tmp = e.WorkModel.ListData(
		limits,
		limitItems,
		order,
		settlementStatus,
		settlementMethod,
		paymentMethod,
		invoiceStatus,
		settlementDateStart,
		settlementDateEnd,
		invoiceDateStart,
		invoiceDateEnd,
		paymentDateStart,
		paymentDateEnd,
		departmentEmployeeIds,
	)
	if totalCounts == 0 {
		return 0, nil
	}
	var contactPersonIds, projectIds []int
	for _, v := range tmp {
		if v.Type == 1 && v.ProjectId != 0 {
			projectIds = append(projectIds, v.ProjectId)
		}
		contactPersonIds = append(contactPersonIds, v.ContactPersonId)
	}

	var contactPersonMap = make(map[int]string)
	var projectMap = make(map[int]string)
	if len(contactPersonIds) > 0 {
		contactPersonCurd := contactPersonCurd.CreateEmployeeCurdFactory()
		contactPerson := contactPersonCurd.GetEmployeeByIds(contactPersonIds)
		for _, v := range contactPerson {
			contactPersonMap[int(v.Id)] = v.Name
		}

	}
	if len(projectIds) > 0 {
		projectCurd := projectCurd.CreateProjectCurdFactory()
		project := projectCurd.GetProjectByIds(projectIds)
		for _, v := range project {
			projectMap[int(v.Id)] = v.ProjectName
		}
	}

	for _, v := range tmp {

		v.ContactPersonName = contactPersonMap[v.ContactPersonId]

		if v.Type == 1 && v.ProjectId != 0 {
			v.ProjectName = projectMap[v.ProjectId]
		}
	}
	return totalCounts, tmp
}

func (e *WorkCurd) ShowOneItem(id int) (tmp *work.WorkModel) {
	tmp = e.WorkModel.ShowOneItem(id)
	if tmp == nil {
		return nil
	}
	employeeService := contactPersonCurd.CreateEmployeeCurdFactory()
	projectService := projectCurd.CreateProjectCurdFactory()
	employee := employeeService.GetOneDataById(tmp.ContactPersonId)
	if employee != nil {
		tmp.ContactPersonName = employee.Name
	}
	project := projectService.GetOneDataById(tmp.ProjectId)
	if project != nil {
		tmp.ProjectName = project.ProjectName
	}
	return tmp
}

func (e *WorkCurd) GetWorkByIds(ids []int) (tmp []*work.WorkModel) {
	return e.WorkModel.GetWorkByIds(ids)
}

func (e *WorkCurd) TotalStatistics() (totalStatistics *work.TotalStatistics) {
	return e.WorkModel.TotalStatistics()
}

func (e *WorkCurd) DeleteData(id int) bool {
	return e.WorkModel.DeleteData(id)
}